package com.sixmac.controller;

import com.sixmac.entity.*;
import com.sixmac.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartRequest;

import javax.servlet.ServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/3/9 0009.
 */
@Controller
@RequestMapping(value = "common")
public class CommonsController {

    @Autowired
    private AreasService areasService;

    @Autowired
    private ImageService imageService;

    @Autowired
    private RolesService rolesService;

    @Autowired
    private WxUserService wxUserService;

    @Autowired
    private AppUserService appUserService;

    /**
     * 权限列表
     *
     * @return
     */
    @RequestMapping("/roleList")
    @ResponseBody
    public List<Roles> roleList() {
        return rolesService.findAll();
    }

    /**
     * 区域列表
     *
     * @return
     */
    @RequestMapping("/areaList")
    @ResponseBody
    public List<Area> areaList() {
        return areasService.findAll();
    }

    /**
     * 微信用户列表
     *
     * @return
     */
    @RequestMapping("/wxUserList")
    @ResponseBody
    public List<WxUser> wxUserList(String nickName) {
        return wxUserService.findList(nickName);
    }

    /**
     * APP用户列表
     *
     * @return
     */
    @RequestMapping("/appUserList")
    @ResponseBody
    public List<AppUser> appUserList(String mobile) {
        return appUserService.findList(mobile);
    }

    /**
     * 上传缓存图片
     *
     * @return
     */
    @RequestMapping("/addTempImage")
    @ResponseBody
    public Image addTempImage(ServletRequest request, MultipartRequest multipartRequest) {
        Image image = new Image();

        try {
            MultipartFile multipartFile = multipartRequest.getFile("tempImage");

            // 验证图片格式
            String originalFileName = multipartFile.getOriginalFilename().toLowerCase();
            String fileType = originalFileName.substring(originalFileName.indexOf("."));

            List<String> list = new ArrayList<String>();
            list.add(".jpg");
            list.add(".gif");
            list.add(".jpeg");
            list.add(".png");
            list.add(".bmp");

            if (!list.contains(fileType)) {
                return image;
            }

            /*String url = QiNiuUploadImgUtil.upload(multipartFile);
            image.setPath(url);*/

            imageService.create(image);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return image;
    }
}
